package auctionhaus



import grails.test.mixin.*
import org.junit.*
import org.grails.datastore.mapping.model.types.Simple

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Listing)
@Mock([Customer, Bid])
class ListingTests {
    def newAndCurrentBid
    def seller

    public void setUp() {
        mockForConstraintsTests(Listing)
        newAndCurrentBid = new Bid(
                amount: 1,
                timeOfBid: new Date()
        )
        seller = new Customer(
                email: "johndoe@acme.com",
                password: "1234567"
        )
    }

    void testNameConstraints() {
        //test blank name
        def blankListing = new Listing(
                endTime: new Date() + 20,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert !blankListing.validate()

        //test too long of a name
        def longListing = new Listing(
                name: "12345678901234567890123456789012345678901234567890123456789012345",
                endTime: new Date() + 20,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert !longListing.validate()

        //test name at exactly 64
        def exactListing = new Listing(
                name: "1234567890123456789012345678901234567890123456789012345678901234",
                endTime: new Date() + 20,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert exactListing.validate()

        def normalListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 20,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert normalListing.validate()
    }

    void testEndTimeConstraints() {
        //test blank end date
        def blankTimeListing = new Listing(
                name: "My awesome pocket lint!",
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert !blankTimeListing.validate()
        
        //test past date
        def pastTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() - 1,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert !pastTimeListing.validate()
        
        //test today's date
        def nowTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date(),
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert !nowTimeListing.validate()
        
        //test future date
        def futureTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 1,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert futureTimeListing.validate()
    }
    
    void testDescriptionConstraints() {
        //test blank description
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert listing.validate()
        
        //test over 256 character limit
        listing.description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ligula dui, vehicula ac vulputate ut, bibendum scelerisque dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris sagittis bibendum orci ut venen"
        assert !listing.validate()

        //test at 256 character limit
        listing.description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ligula dui, vehicula ac vulputate ut, bibendum scelerisque dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris sagittis bibendum orci ut v"
        assert listing.validate()

        //test a normal description
        listing.description = "Etiam fermentum consectetur varius. Aenean et leo nec dolor eleifend bibendum. In dictum neque et erat egestas a fermentum mauris posuere."
        assert listing.validate()
    }

    void testSellerConstraints() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                startBid: newAndCurrentBid
        )
        assert !listing.validate()

        listing.seller = seller
        assert listing.validate()
    }

    void testNullableWinner() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
                startBid: newAndCurrentBid
        )
        assert listing.validate()
    }

    void testStartBidConstraints() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
        )
        assert !listing.validate()

        listing.startBid = newAndCurrentBid
        assert listing.validate()
    }

    public void tearDown() {
        newAndCurrentBid = null
        seller = null
    }
}
